﻿#region

using System.Reflection;
using SqlSugar;
using Vin.Extension.SqlSugar.Attributes;
using Vin.Extension.SqlSugar.DataExecute;
using Vin.Extension.SqlSugar.SugarFilter;
using Vin.Tool.Core.AssertCategory;
using Vin.Tool.Core.JsonCategory;

#endregion

namespace Vin.Extension.SqlSugar.Extension;

public static class EncryptExtension
{
    public static void AddEncrypt(this VinSqlSugarClient client)
    {
        client.VinAop.DataExecuting = DataExecuting;
        client.VinAop.DataExecuted = DataExecuted;
    }

    // 数据处理中
    private static void DataExecuting(ISqlSugarClient client, object? oldValue, DataFilterModel entityInfo)
    {
        if (oldValue == null || (oldValue is string s && s.IsNullOrEmpty())) return;

        var encryptAttribute = entityInfo.GetAttribute<VinEncryptAttribute>();
        if (encryptAttribute == null) return;

        // 插入 || 更新
        if (entityInfo.OperationType == DataFilterType.InsertByObject ||
            entityInfo.OperationType == DataFilterType.UpdateByObject)
        {
            if (encryptAttribute.Adapter != null)
            {
                var adapter = Activator.CreateInstance(encryptAttribute.Adapter) as IVinSugarBaseEncryptAdapter;
                if (adapter == null) return;
                entityInfo.SetValue(adapter.Encrypt(oldValue.ToCamelCaseJson()));
            }

            // var propertyInfo = entityInfo.EntityValue.GetType().GetProperty(entityInfo.PropertyName);
            // var value = propertyInfo?.GetValue(entityInfo.EntityValue);
            // if (propertyInfo == null || value == null) return;
            //
            // var adapter = Activator.CreateInstance(encryptAttribute.Adapter) as IVinSugarBaseEncryptAdapter;
            // if (adapter == null) return;
            // propertyInfo.SetValue(entityInfo.EntityValue,
            //     adapter.Encrypt(value.ToJson()));
        }

        // 删除
        if (entityInfo.OperationType == DataFilterType.DeleteByObject)
        {
        }
    }

    private static void DataExecuted(ISqlSugarClient client, object? value, DataAfterModel entity)
    {
        var haveAttributeColumns =
            entity.EntityColumnInfos.Where(x =>
                x.PropertyInfo.GetValue(value) != null &&
                x.PropertyInfo.GetCustomAttribute<VinEncryptAttribute>() != null);

        foreach (var column in haveAttributeColumns)
        {
            var columnValue = column.PropertyInfo.GetValue(value);
            if (columnValue == null) continue;

            var encryptAttribute = column.PropertyInfo.GetCustomAttribute<VinEncryptAttribute>();
            if (encryptAttribute == null) continue;

            if (encryptAttribute.Adapter != null)
            {
                var adapter = Activator.CreateInstance(encryptAttribute.Adapter) as IVinSugarBaseEncryptAdapter;
                if (adapter == null) continue;
                entity.SetValue(column.PropertyName, adapter.Decrypt(columnValue.ToCamelCaseJson()));
            }
        }
    }
}